package mx.com.eit.Testing;

public class TestThread extends Thread {

	long TMem = 0;
	int TNum = 0;
	int PLimit = 0;
	boolean TPrinted = false;
	
	public TestThread(int num, int limit){
		this.TMem = getMemory();
		this.TNum = num;
		this.PLimit = limit;
	}
	
	public void run(){
		System.out.println("Thread"+TNum+" Mem: "+TMem);
		int count = 0;
		for (int i = 1; i<this.PLimit; i++){
			if (isPrimeNumber(i)){count++;}
			try {
				sleep(100*this.TNum);
			} catch (InterruptedException e) {
				break;
			}
		}
		System.out.println("There are "+ count +" prime numbers between 1 and "+ this.PLimit);
		this.TMem = getMemory();
		System.out.println("DONE Thread"+TNum+" Mem: "+TMem);
	}
	
	private boolean isPrimeNumber(int x){
		boolean isp = true;
		for (int j = 2; j<x; j++){
			if ((x % j) == 0){
				isp = false;
				break;
			}
		}
		return isp;
	}
	
	private long getMemory(){
		long mem = 0;
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		return mem / 1024;
	}
}
